package cn.s1995.sloth.bms.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.log.annotation.SysLog;
import cn.s1995.sloth.bizs.entity.Country;
import cn.s1995.sloth.bizs.service.CountryService;
import org.springframework.security.access.prepost.PreAuthorize;
import cn.s1995.sloth.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 国家区域
 *
 * @author Lex
 * @date 2023-12-12 01:33:21
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/country")
@Tag(description = "Country", name = "BMS-国家区域管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CountryController {

    private final CountryService countryService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param req 国家区域
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('bizs_Country_view')")
    public R getCountryPage(@ParameterObject Page page, @ParameterObject Country req) {
        LambdaQueryWrapper<Country> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(StrUtil.isNotBlank(req.getName()), Country::getName, req.getName());
		wrapper.eq(Objects.nonNull(req.getCode()), Country::getCode, req.getCode());
		wrapper.eq(StrUtil.isNotBlank(req.getLang()), Country::getLang, req.getLang());
		wrapper.eq(Objects.nonNull(req.getIsHot()), Country::getIsHot, req.getIsHot());
        return R.ok(countryService.page(page, wrapper));
    }


    /**
     * 通过id查询国家区域
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('bizs_Country_view')")
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(countryService.getById(id));
    }

    /**
     * 新增国家区域
     * @param req 国家区域
     * @return R
     */
    @Operation(summary = "新增国家区域", description = "新增国家区域")
    @SysLog("新增国家区域")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('bizs_Country_add')")
    public R save(@RequestBody Country req) {
        return R.ok(countryService.save(req));
    }

    /**
     * 修改国家区域
     * @param req 国家区域
     * @return R
     */
    @Operation(summary = "修改国家区域", description = "修改国家区域")
    @SysLog("修改国家区域")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('bizs_Country_edit')")
    public R updateById(@RequestBody Country req) {
        return R.ok(countryService.updateById(req));
    }

    /**
     * 通过id删除国家区域
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除国家区域", description = "通过id删除国家区域")
    @SysLog("通过id删除国家区域")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('bizs_Country_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(countryService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param req 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @Operation(summary = "导出excel表格", description = "")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('bizs_Country_export')")
    public List<Country> export(Country req, Long[] ids) {
        return countryService.list(Wrappers.lambdaQuery(req).in(ArrayUtil.isNotEmpty(ids), Country::getId, ids));
    }
}
